﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MathLib {
    public class RauseGurvitzCriterion {

        public static bool CheckStability(double[] coef) {
            if (coef[0] < 0)
                for (int i = 0; i < coef.Length; i++)
                    coef[i] *= -1;
            int n = coef.Length - 1;
            Matrix gurv = Matrix.Zero(n);

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) {
                    int k = 2 * j + 1 - i;
                    if (0 <= k && k <= n)
                        gurv[i, j] = coef[k];
                }

            for (int i = 0; i < n; i++)
                if (GetCornerMinor(gurv, i) <= 0)
                    return false;
            return true;
        }

        private static double GetCornerMinor(Matrix m, int k) {
            Matrix a = new Matrix(k, k);
            for (int i = 0; i < k; i++)
                for (int j = 0; j < k; j++)
                    a[i, j] = m[i, j];
            return Matrix.Det(a);
        }
    }
}